<template>
  <el-cascader
    size="mini"
    :value="value"
    :options="treeData"
    :props="porps"
    @change="handleChange"
  ></el-cascader>
</template>
<script>
//引入接口
import { getDepartmentList } from "@/api/department";
//引入工具函数
import { listToTree } from "@/utils";
export default {
  name: "tree",
  data() {
    return {
      treeData: [],

      porps: {
        value: "id",
        label: "name",
      },
    };
  },
  //挂载时获取
  mounted() {
    this.getDepartmentList();
  },
  methods: {
    //获取数据的方法
    async getDepartmentList() {
      let reslut = await getDepartmentList();
      //存储数据
      this.treeData = listToTree(reslut, 0);
    },
    handleChange(list) {
       if(list.length >0) {
        this.$emit('input',list[list.length-1])
       }else {
        this.$emit('input',null)
       }
    }
  },
  props: {
    value: {
      type: Number, // 存储的是部门的id  3 4 5
      default: null,
    },
  },
};
</script>
